//
//  ZGLAddNewChannelCell.m
//  Faker
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLAddNewChannelCell.h"
#import "ZGLSearchHeadUpModel.h"
#import <UIImageView+AFNetworking.h>

@interface ZGLAddNewChannelCell ()
@property (nonatomic, strong) NSMutableDictionary *signDic;
@property (nonatomic, strong) NSMutableArray *signArr;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIButton *selectButton;
@property (nonatomic, strong) UILabel *boderLabel;

@end

@implementation ZGLAddNewChannelCell

- (void)dealloc {
    [_selectButton removeTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftImageView = [[UIImageView alloc] init];
        self.titleLabel = [[UILabel alloc] init];
        self.boderLabel = [[UILabel alloc] init];
        self.selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.selectButton];
        [self.contentView addSubview:self.boderLabel];
        [self.contentView addSubview:self.leftImageView];
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.leftImageView setFrame:CGRectMake(0, ScaleY * 10, ScaleX * 120, ScaleY * 120)];
    [self.titleLabel setFrame:CGRectMake(ScaleX * 140, ScaleY * 40, ScaleX * 200, ScaleY * 30)];
    [self.boderLabel setFrame:CGRectMake(ScaleX * 15, ScaleY * 15, ScaleX * 90, ScaleY * 90)];
    [self.selectButton setFrame:CGRectMake(ScaleX * 320, ScaleY * 40, ScaleX * 30, ScaleY * 30)];
    [self.selectButton setImage:[UIImage imageNamed:@"tianjia"] forState:(UIControlStateNormal)];
    [self.selectButton setImage:[UIImage imageNamed:@"sdingyue"] forState:(UIControlStateSelected)];
    if ([self judgeIsYESORNO]) {
        _selectButton.selected = YES;
    } else {
        _selectButton.selected = NO;
    }
    [self.selectButton addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    self.boderLabel.layer.cornerRadius = ScaleX * 45;
    self.boderLabel.layer.masksToBounds = YES;
    self.boderLabel.layer.borderWidth = 1;
    self.titleLabel.font = [UIFont systemFontOfSize:17];

}

- (BOOL)judgeIsYESORNO {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    for (NSDictionary *dic in self.signArr) {
        if ([dic[@"title"] isEqualToString:self.titleLabel.text]) {
            return YES;
        }
    }
    return NO;
}
- (void)button:(UIButton *)button {
    if (button.selected == YES) {
        [button setSelected:NO];
        [self delegateLocalSignplist];
    } else {
        
        [button setSelected:YES];
        [self readLocalSignplist];
    }
}
#pragma mark - 沙盒plist写入
- (void)readLocalSignplist {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:_searchModel.title forKey:@"title"];
    [dic setObject:_searchModel.large_pic forKey:@"large_pic"];
    [dic setObject:_searchModel.block_color forKey:@"block_color"];
    [dic setObject:_searchModel.api_url forKey:@"api_url"];
    [self.signArr addObject:dic];
    [userDefaults setObject:_signArr forKey:@"currentArr"];
    [userDefaults synchronize];
}

- (void)delegateLocalSignplist {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.signArr = [userDefaults arrayForKey:@"currentArr"].mutableCopy;
    NSMutableArray *newArr = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _signArr) {
        if ([dic[@"title"] isEqualToString:_searchModel.title]) {
            
        } else {
            [newArr addObject:dic];
        }
    }
    [userDefaults setObject:newArr forKey:@"currentArr"];
}


- (void)setSearchModel:(ZGLSearchHeadUpModel *)searchModel {
    if (_searchModel != searchModel) {
        _searchModel = searchModel;
    }
    [self createOptionItemData:searchModel];
    self.titleLabel.text = searchModel.title;
}

- (void)createOptionItemData:(ZGLSearchHeadUpModel *)searchModel {
    __weak typeof(self) weakSelf = self;
    self.leftImageView.tintColor = [self getColor:searchModel.block_color];
    self.boderLabel.layer.borderColor = [self getColor:searchModel.block_color].CGColor;
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:searchModel.large_pic]];
    [self.leftImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        [weakSelf.leftImageView setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        
    }];
}

- (UIColor *)getColor:(NSString *)hexColor
{
    NSString *string = [hexColor substringFromIndex:1];//去掉#号
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    red = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 2;
    green = [self stringToInt:[string substringWithRange:range]];
    
    range.location = 4;
    blue = [self stringToInt:[string substringWithRange:range]];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}

- (int)stringToInt:(NSString *)string{
    
    unichar hex_char1 = [string characterAtIndex:0]; ////两位16进制数中的第一位(高位*16)
    int int_ch1;
    if(hex_char1 >= '0' && hex_char1 <='9')
        int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
    else
        int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
    
    unichar hex_char2 = [string characterAtIndex:1]; ///两位16进制数中的第二位(低位)
    int int_ch2;
    if(hex_char2 >= '0' && hex_char2 <='9')
        int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
    else if(hex_char1 >= 'A' && hex_char1 <='F')
        int_ch2 = hex_char2-55; //// A 的Ascll - 65
    else
        int_ch2 = hex_char2-87; //// a 的Ascll - 97
    return int_ch1+int_ch2;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
